﻿@using GlobalResources
@using Microsoft.Azure.Devices
@using Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security
@using Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Extensions;
@using Microsoft.Azure.Devices.Applications.RemoteMonitoring.Common.Extensions;
@model Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Models.DeviceJobModel

@if (PermsChecker.HasPermission(Permission.ManageJobs))
{
    bool hasActions = false;

    <div class="header_grid header_grid_general">
        <h3 class="grid_subheadhead_detail">@Strings.Actions</h3>
    </div>

    <section class="details_grid_actions">
        @if (!string.IsNullOrEmpty(Model.FilterId) && !Model.IsIconJob){
            <p class="grid_detail_value">
                @Html.ActionLink(Strings.CloneJobAction, "CloneJob", "Job", new { jobId = Model.JobId }, new { id = "queryLink" })
            </p>
            hasActions = true;
        }

        @if (Model.Status == JobStatus.Scheduled || Model.Status == JobStatus.Enqueued || Model.Status == JobStatus.Queued)
        {
            <p class="grid_detail_value">
                <a id="cancelJobAction">
                   @Strings.CancelJobAction
                </a>
            </p>
            hasActions = true;
        }

        @if (!hasActions)
        {
            <p class="grid_detail_value">@Strings.NoAvailableActions</p>
        }
    </section>
}

<div class="header_grid header_grid_general">
    <img src="~/Content/img/expanded.svg" class="details_grid_info pull-left cursor_pointer toggle_source toggle_target" />
    <img src="~/Content/img/collapsed.svg" class="details_grid_info pull-left cursor_pointer toggle_source toggle_target display_none" />
    <h3 class="grid_subheadhead_detail toggle_source">@Strings.JobProperties</h3>
</div>

<section class="details_grid_general toggle_target" id="jobDetailsGrid">
    <h4 class="grid_subhead_detail_label grid_subhead_detail_label_job_property">@Strings.JobId</h4>
    <p class="grid_detail_value" name="deviceField_jobId">@Model.JobId</p>

    <h4 class="grid_subhead_detail_label grid_subhead_detail_label_job_property">@Strings.StatusHeader</h4>
    <p class="grid_detail_value" name="deviceField_status">@Model.Status</p>

    <h4 class="grid_subhead_detail_label grid_subhead_detail_label_job_property">@Strings.JobNameHeader</h4>
    <p class="grid_detail_value" name="deviceField_jobName">@Model.JobName</p>

    <h4 class="grid_subhead_detail_label grid_subhead_detail_label_job_property">@Strings.FilterNameHeader</h4>
    <p class="grid_detail_value" name="deviceField_filterName">
        @if (Model.FilterName.Equals(Model.QueryCondition, StringComparison.InvariantCultureIgnoreCase))
        {
            @Model.FilterName
        }
        else
        {
            @Html.ActionLink(Model.FilterName, "Index", "Device", new { filterId = Model.FilterId }, new { id = "filterLink" })
        }
    </p>
</section>

<div class="header_grid header_grid_general">
    <img src="~/Content/img/expanded.svg" class="details_grid_info pull-left cursor_pointer toggle_source toggle_target" />
    <img src="~/Content/img/collapsed.svg" class="details_grid_info pull-left cursor_pointer toggle_source toggle_target display_none" />
    <h3 class="grid_subheadhead_detail toggle_source">@Strings.JobOperationResult</h3>
</div>

<section class="details_grid_general details_grid_general_section_job" id="jobOperationGrid"> 
    @if (Model.IsMethodJob)
    {
        <h4 class="grid_subhead_detail_label" name="deviceField_operationType">
            @Strings.InvokeMethod
            (@Model.CloudToDeviceMethod.MethodName)
        </h4>
        <input type="hidden" id="payloadBox" value="@Model.CloudToDeviceMethod.GetPayloadAsJson()"/>
        <ul class="details_grid_job_method_payload"></ul>
    }
    else if (Model.IsTwinJob)
    {
        <h4 class="grid_subhead_detail_label" name="deviceField_operationType">
           @Strings.ScheduleEditPropertiesOrTags
        </h4>

        <div>
            @foreach (var tag in Model.UpdateTwin.Tags.AsEnumerableFlatten("tags.", false))
            {
            <ul class="job_result_section__twin_list">
                @if (tag.Value.Value.Type != Newtonsoft.Json.Linq.JTokenType.Null)
                {
                    <li>@tag.Key</li>
                    <li>@tag.Value.Value.Value</li>
                }
                else
                {
                    <li class="job_result_section__twin_list_crossline">@tag.Key</li>
                    <li></li>
                }
            </ul>
            }

            @foreach (var property in Model.UpdateTwin.Properties.Desired.AsEnumerableFlatten("desired.", false))
            {
            <ul class="job_result_section__twin_list">
                @if (property.Value.Value.Type != Newtonsoft.Json.Linq.JTokenType.Null)
                {
                    <li>@property.Key</li>
                    <li>@property.Value.Value.Value</li>
                }
                else
                {
                    <li class="job_result_section__twin_list_crossline">@property.Key</li>
                    <li></li>
                }
            </ul>
            }
        </div>
    }
    else if (Model.IsIconJob)
    {
        <p class="grid_detail_value" name="deviceField_status">@Model.OperationType</p>
    }
</section>

<div class="header_grid header_grid_general">
    <img src="~/Content/img/expanded.svg" class="details_grid_info pull-left cursor_pointer toggle_source toggle_target" />
    <img src="~/Content/img/collapsed.svg" class="details_grid_info pull-left cursor_pointer toggle_source toggle_target display_none" />
    <h3 class="grid_subheadhead_detail toggle_source">@Strings.JobStatistics</h3>
    <img src="~/Content/img/icon_info_gray.svg" class="details_grid_info" title="@Strings.JobStatisticsTooltip" />
    <a class="link_grid_subheadhead_detail" id="lnkViewJobResult">@Strings.ShowJobResults</a>
</div>

<section class="details_grid_general details_grid_general_section_job" id="deviceGrid">
    <h4 class="grid_subhead_detail_label grid_subhead_detail_label_job_property">@Strings.DeviceCountSubHeader</h4>
    <p class="grid_detail_value" name="deviceField_deviceCount">@Model.DeviceCount</p>

    <h4 class="grid_subhead_detail_label grid_subhead_detail_label_job_property">@Strings.SucceededCountSubHeader</h4>
    <p class="grid_detail_value grid_detail_value_with_link" name="deviceField_succeedCount">@Model.SucceededCount</p>

    <h4 class="grid_subhead_detail_label grid_subhead_detail_label_job_property">@Strings.FailedCountSubHeader</h4>
    <p class="grid_detail_value grid_detail_value_with_link" name="deviceField_failedCount">@Model.FailedCount</p>

    <h4 class="grid_subhead_detail_label grid_subhead_detail_label_job_property">@Strings.PendingCountSubHeader</h4>
    <p class="grid_detail_value grid_detail_value_with_link" name="deviceField_pendingCount">@Model.PendingCount</p>

    <h4 class="grid_subhead_detail_label grid_subhead_detail_label_job_property">@Strings.RunningCountSubHeader</h4>
    <p class="grid_detail_value grid_detail_value_with_link" name="deviceField_runningCount">@Model.RunningCount</p>
</section>
